package com.common.util;

import java.io.Serializable;

/**
 * 封装响应结果
 * 
 * @Author IanZy
 * @Date 2020/6/15
 */
public class ResultMsg<T> implements Serializable {
    //封装状态码
    private Integer code;
    //封装响应消息
    private String msg;
    //封装响应数据
    private T data;

    public ResultMsg() {
    }



    //返回状态码和消息
    public ResultMsg(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    //返回消息
    public ResultMsg(String msg) {
        this.msg = msg;
    }

    //返回状态码，消息，数据
    public ResultMsg(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResultMsg(T data){
        this.data=data;
    }

    //响应成功，返回状态码和消息
    public static ResultMsg success() {
        return new ResultMsg(ResultUtil.SUCCESS.getCode(), ResultUtil.SUCCESS.getMsg());
    }

    //响应成功，返回状态码，消息和数据
    public static <T> ResultMsg success(String msg, T data) {
        return new ResultMsg(ResultUtil.SUCCESS.getCode(), msg, data);
    }

    //自定义响应成功
    public static ResultMsg success(String msg) {
        return new ResultMsg(ResultUtil.SUCCESS.getCode(), msg);
    }

    //响应成功
    public static <T> ResultMsg success(T data) {
        return new ResultMsg(ResultUtil.SUCCESS.getCode(), ResultUtil.SUCCESS.getMsg());
    }

    //自定义响应失败
    public static ResultMsg failed(String msg) {
        return new ResultMsg(ResultUtil.FAILED.getCode(), msg);
    }

    //响应失败
    public static ResultMsg failed() {
        return new ResultMsg(ResultUtil.FAILED.getCode(), ResultUtil.FAILED.getMsg());
    }

    //参数验证失败
    public static ResultMsg validateFailed() {
        return new ResultMsg(ResultUtil.VALIDATE_FAILED.getCode(), ResultUtil.VALIDATE_FAILED.getMsg());
    }

    //未授权
    public static ResultMsg forbiddenFailed() {
        return new ResultMsg(ResultUtil.FORBIDDEN.getCode(), ResultUtil.VALIDATE_FAILED.getMsg());
    }

    public static ResultMsg unauthorizedFailed() {
        return new ResultMsg(ResultUtil.UNAUTHORIZED.getCode(), ResultUtil.UNAUTHORIZED.getMsg());
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
